/*
 * main.cpp
 *
 *   Created on: 7.01.2011
 *       Author: Mike Shatohin (brunql)
 *
 *      Project: USBtinyThermometer
 *
 *  Copyright (c) 2011 Mike Shatohin, mikeshatohin [at] gmail.com
 *
 *  USBtinyThermometer is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  USBtinyThermometer is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <QtCore>
#include <QtNetwork>

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <string>

#include "httprequest.h"
#include "thermometerdevice.h"

#include "version.h"


static void printUsage()
{
    fprintf(stderr, "\n");
    fprintf(stderr, "Read and send temperature requested from USB device\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Project: USBtinyThermometer\n");
    fprintf(stderr, "Version: %s\n", VERSION_STR);
    fprintf(stderr, "Author: Mike Shatohin (brunql)\n");
    fprintf(stderr, "Project: usb-tiny-thermometer.googlecode.com \n");
    fprintf(stderr, "Copyleft (c) 2011\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Usage: USBtinyThermometer [-v] [-h] [-1 | -2 | -a] [-s] \n");
    fprintf(stderr, "Example: USBtinyThermometer -2 -v \n");
    fprintf(stderr, "  -h, --help       show this help\n");
    fprintf(stderr, "  -v, --verbose    vebose output\n");
    fprintf(stderr, "  -f, --noround    12-bit result, 4 bits fractional part (+26,3125)\n");
    fprintf(stderr, "  -1, --first      measure first sensor (default)\n");
    fprintf(stderr, "  -2, --second     measure second sensor\n");
    fprintf(stderr, "  -a, --all        measure first and second sensor\n");
    fprintf(stderr, "  -s, --send       send results to host\n");
    fprintf(stderr, "  -c, --readline   readline before exit\n");
    fprintf(stderr, "\n");
    fflush(stderr);
}


int verboseLevel = 0;


void out(const char *str)
{
    if(verboseLevel){
        printf("%s", str);
        fflush(stdout);
    }
}

int main(int argc, char **argv)
{
    QCoreApplication a(argc, argv);

    // Defaults:
    bool round_to_one_decimal = true;
    int sensor_index = FIRST_SENSOR;
    bool http_send_results = false;
    bool read_line = false;

    QStringList args = a.arguments();

    for(int i=1; i<args.count(); i++){
        if(args[i] == "-v" || args[i] == "--verbose"){
            verboseLevel++;
        }else if(args[i] == "-f" || args[i] == "--noround"){
            round_to_one_decimal = false;
        }else if(args[i] == "-r" || args[i] == "--round"){
            round_to_one_decimal = true;
        }else if(args[i] == "-1" || args[i] == "--first"){
            sensor_index = FIRST_SENSOR;
        }else if(args[i] == "-2" || args[i] == "--second"){
            sensor_index = SECOND_SENSOR;
        }else if(args[i] == "-a" || args[i] == "--all"){
            sensor_index = ALL_SENSORS;
        }else if(args[i] == "-s" || args[i] == "--send"){
            http_send_results = true;
        }else if(args[i] == "-c" || args[i] == "--readline"){
            read_line = true;
        }else if(args[i] == "-h" || args[i] == "--help"){
            printUsage();
            exit(0);
        }else{
            qDebug() << "\nBad command line option:" << args[i];
            printUsage();
            exit(1);
        }
    }

    HttpRequest http( verboseLevel );

    ThermometerDevice device( verboseLevel );

    int number_of_sensors = device.findSensors();

    if(number_of_sensors == 0){
        out("Error: haven't any sensors\n");
        exit(1);
    }else if(number_of_sensors < 2 && sensor_index == SECOND_SENSOR){
        out("Error: have only one sensor\n");
        exit(2);
    }


    device.startMeasurement();


    double temp = 0; // this temp will send to host
    QString sensor_id = ""; // this id with 'temp' will send to host

    if(sensor_index == ALL_SENSORS && number_of_sensors != 2){
        sensor_index = FIRST_SENSOR;
    }

    if(sensor_index == ALL_SENSORS){
        temp = device.getThemperature(FIRST_SENSOR, round_to_one_decimal);
        sensor_id = device.getSensorId(FIRST_SENSOR);
        double temp2 = device.getThemperature(SECOND_SENSOR, round_to_one_decimal);

        out("Temperatures: ");
        if(round_to_one_decimal){
            printf("%+.1f  %+.1f\n", temp, temp2);
        }else{
            printf("%+.4f  %+.4f\n", temp, temp2);
        }
    }else{
        temp = device.getThemperature(sensor_index, round_to_one_decimal);
        sensor_id = device.getSensorId(sensor_index);

        out("Temperature: ");
        if(round_to_one_decimal){
            printf("%+.1f\n", temp);
        }else{
            printf("%+.4f\n", temp);
        }
    }
    out("\n");

    device.close();


    if(http_send_results){
        out("Send temperature to host:\n");
        http.sendTemperature(sensor_id, temp);
    }

    if(read_line){
        std::string str;
        std::getline(std::cin, str);
    }

    if(http_send_results == false){
        exit(0);
    }

    // When http emit-s httpFinished() program will terminated
    return a.exec();
}











